using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IronPython.Hosting;
using Microsoft.Scripting;
using IronPython.Runtime;
using Microsoft.Scripting.Hosting;

namespace EngineExecuteFileDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private ScriptEngine engine;
        private ScriptScope scope;

        private void button1_Click(object sender, EventArgs e)
        {

            if (engine == null)
            {
                engine = PythonEngine.CurrentEngine;
                //ScriptModule defaultModule = (ScriptModule)ScriptDomainManager.CurrentManager.Host.DefaultModule;
                engine.Options.ExceptionDetail = true;
                scope = engine.Runtime.CreateScope();
                scope.SetVariable("win", this);
                //defaultModule.SetVariable("win", this);
                //engine.AddToPath(Environment.CurrentDirectory);
                engine.SetScriptSourceSearchPaths(new string[] { Environment.CurrentDirectory });
                System.IO.FileStream stdin = new System.IO.FileStream("scripting-log.txt", System.IO.FileMode.OpenOrCreate);
                System.IO.FileStream stdout = new System.IO.FileStream("scripting-out.txt", System.IO.FileMode.OpenOrCreate);
                System.IO.FileStream stderr = new System.IO.FileStream("scripting-error.txt", System.IO.FileMode.OpenOrCreate);
                engine.Runtime.IO.SetInput(stdin,Encoding.Default);
                engine.Runtime.IO.SetOutput(stdout, Encoding.Default);
                engine.Runtime.IO.SetErrorOutput(stderr,Encoding.Default);
                //SystemState.Instance.stdout = new PythonFile(stdin, Encoding.Default, "<stdout>", "w");
                //SystemState.Instance.stderr = new PythonFile(stderr, Encoding.Default, "<stderr>", "w"); 
            }
            try
            {
                string pathFile = "test.py";
                engine.CreateScriptSourceFromFile(pathFile, Encoding.Default).Execute(scope);
                //engine.Execute(scope, tempFile1);
                //engine.Execute("test.py", defaultModule);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }           
           
        }
    }
}